// PART 2 PRODUCES RESULTS FOR TABLE 7

clear
cd "\\file\UsersW$\wrr15\Home\My Documents\My Files\XINDONG XUE\META-ANALYSIS\REVISION FOR JHE\DATA AND CODE"
set more off
set type double
graph drop _all
program drop _all
set scheme s2mono
//log using "Part2 Results(20191130).smcl", replace
use "sc_data(20191130).dta"

******************************************************************************
*DEFINITION OF BASIC VARIABLES
******************************************************************************
gen tstat = real(tstatistics)
gen negative = 1
replace negative = -1 if ((badhealth == 1 & badsc == 0) | (badhealth == 0 & badsc == 1))
replace tstat = tstat*negative
gen df = nobs
gen obsno = _n
replace df = . if tstat == .
gen r = tstat/sqrt(tstat^2+df)
gen varR = (1-r^2)/df
gen seR = sqrt(varR)
gen pcc = r

quietly summ pcc, detail
keep if pcc > r(p1) & pcc < r(p99) 

// Calculating study weights
bysort id: egen numberests = count(id)
tabulate numberests 
gen weight = 1/numberests

quietly summ pubyear
replace pubyear = pubyear - r(mean)

quietly summ numberofscvariables
replace numberofscvariables = numberofscvariables - r(mean)

gen eastasia=(country_region=="East_Asia")
gen usa=(country_region=="USA")
gen westnortheurope=(country_region=="West_North_Europe")
gen highincome=(country_region=="High-income countries")
gen othercountry = 1 - eastasia - usa - westnortheurope - highincome

quietly summ numberofscvariables
// We replace numberofscvariables with deviation from mean
replace numberofscvariables = numberofscvariables - r(mean)

// Estimation Method
gen otherestimation = 1 - ols - fgls - probitlogit - orderedprobitlogit - orhr - hlm - iv 

gen tcalculatedbyci=(tcalculatedbycise==1 & cilowerbound != .)
gen tcalculatedbypvalue2 = tcalculatedbypvalue
replace tcalculatedbypvalue = (tcalculatedbypvalue2 == 1)
gen tnormal = 1 - tcalculatedbyci - tcalculatedbypvalue

// Generating transformed variables for FE and RE
gen fetstatR = r/seR
metareg r seR pubyear panel iv individualsc eastasia westnortheurope highincome othercountry ///
physicalhealth mentalhealth selfreported cognitivestructual bondingbridginglinking ///
numberofscvariables age gender education maritalstatus income ols orhr hlm ///
seother tnormal tcalculatedbypvalue, wsse(seR)
scalar tau2 =  e(tau2)
gen revarR = varR + tau2
gen reseR = sqrt(revarR)
gen retstatR = r/reseR
gen repubbiasR = seR/reseR

*************************************
*************************************
*************************************
************ TABLE 7 ****************
*************************************
*************************************
*************************************

**********************************************
************ FIXED EFFECTS *******************
**********************************************
gen conss = 1/seR
gen pubyearr = pubyear/seR
gen panell = panel/seR
gen ivv = iv/seR
gen individualscc = individualsc/seR
gen eastasiaa = eastasia/seR
gen westnortheuropee = westnortheurope/seR
gen highincomee = highincome/seR
gen othercountryy = othercountry/seR
gen physicalhealthh = physicalhealth/seR
gen mentalhealthh = mentalhealth/seR
gen selfreportedd = selfreported/seR
gen numberofscvariabless = numberofscvariables/seR
gen agee = age/seR
gen genderr = gender/seR
gen educationn = education/seR
gen maritalstatuss = maritalstatus/seR
gen incomee = income/seR
gen olss = ols/seR
gen orhrr = orhr/seR
gen hlmm = hlm/seR
gen seotherr = seother/seR
gen tnormall = tnormal/seR
gen tcalculatedbypvaluee = tcalculatedbypvalue/seR

// NOTE: The constant term is the SER term

*--------------------------------------------------------*
*   TABLE: FixedEffects(Weight1) // Stepwise Regression  *
*--------------------------------------------------------*

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion 
quietly regress fetstatR physicalhealthh mentalhealthh individualscc  panell ivv pubyearr ///
eastasiaa westnortheuropee highincomee othercountryy selfreportedd ///
numberofscvariabless agee genderr educationn maritalstatuss incomee olss orhrr ///
hlmm seotherr tnormall tcalculatedbypvaluee conss 
gen esample = e(sample)

vselect fetstatR pubyearr eastasiaa westnortheuropee highincomee othercountryy ///
selfreportedd numberofscvariabless agee genderr educationn maritalstatuss ///
incomee olss orhrr hlmm seotherr tnormall tcalculatedbypvaluee , ///
backward bic fix(conss physicalhealthh mentalhealthh individualscc panell ivv)

// Fixed Effects - Equal weight to each estimate (Column 1)
regress fetstatR conss physicalhealthh mentalhealthh individualscc panell ivv eastasiaa ///
westnortheuropee highincomee selfreportedd numberofscvariabless agee educationn maritalstatuss  ///
olss seotherr tnormall tcalculatedbypvaluee if esample == 1, vce(cluster id)
display e(r2_a)

test (physicalhealthh mentalhealthh)

*--------------------------------------------------------*
*   TABLE: FixedEffects(Weight2) // Stepwise Regression  *
*--------------------------------------------------------*

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion
drop esample
quietly regress fetstatR physicalhealthh mentalhealthh individualscc  panell ivv pubyearr ///
eastasiaa westnortheuropee highincomee othercountryy selfreportedd ///
numberofscvariabless agee genderr educationn maritalstatuss incomee olss orhrr ///
hlmm seotherr tnormall tcalculatedbypvaluee conss  [pweight = weight]
gen esample = e(sample)

vselect fetstatR pubyearr eastasiaa westnortheuropee highincomee othercountryy ///
selfreportedd numberofscvariabless agee genderr educationn maritalstatuss ///
incomee olss orhrr hlmm seotherr tnormall tcalculatedbypvaluee ///
[pweight = weight] , backward bic fix(conss physicalhealthh mentalhealthh individualscc panell ivv)

// Fixed Effects - Equal weight to each study(Column 2)
regress fetstatR conss physicalhealthh mentalhealthh individualscc  panell ivv pubyearr ///
eastasiaa westnortheuropee highincomee selfreportedd numberofscvariabless  ///
educationn maritalstatuss olss hlmm seotherr tnormall tcalculatedbypvaluee ///
[pweight = weight] if esample == 1 , vce(cluster id)
display e(r2_a)

test (physicalhealthh mentalhealthh)

///////////////////////////////////////////////
//////////////  RANDOM EFFECTS  ///////////////
///////////////////////////////////////////////

replace conss = 1/reseR
replace pubyearr = pubyear/reseR
replace individualscc = individualsc/reseR
replace panell = panel/reseR
replace ivv = iv/reseR
replace eastasiaa = eastasia/reseR
replace westnortheuropee = westnortheurope/reseR
replace highincomee = highincome/reseR
replace othercountryy = othercountry/reseR
replace physicalhealthh = physicalhealth/reseR
replace mentalhealthh = mentalhealth/reseR
replace selfreportedd = selfreported/reseR
replace numberofscvariabless = numberofscvariables/reseR
replace agee = age/reseR
replace genderr = gender/reseR
replace educationn = education/reseR
replace maritalstatuss = maritalstatus/reseR
replace incomee = income/reseR
replace olss = ols/reseR
replace orhrr = orhr/reseR
replace hlmm = hlm/reseR
replace seotherr = seother/reseR
replace tnormall = tnormal/reseR
replace tcalculatedbypvaluee = tcalculatedbypvalue/reseR

*---------------------------------------------------------*
*   TABLE: RandomEffects(Weight1) // Stepwise Regression  *
*---------------------------------------------------------*

// NOTE: For Random Effects models, SE/reseR is not equal to 1. This causes a problem because the
// stepwise regression command does not have a "noconstant" option. Our solution is to run the stepwise
// regression with the constant term, then drop it in the final equation.

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion
drop esample
quietly regress retstatR repubbiasR pubyearr individualscc panell ivv eastasiaa ///
westnortheuropee highincomee othercountryy physicalhealthh mentalhealthh ///
selfreportedd numberofscvariabless agee genderr educationn maritalstatuss ///
incomee olss orhrr hlmm seotherr tnormall tcalculatedbypvaluee conss 
gen esample = e(sample)

vselect retstatR repubbiasR pubyearr eastasiaa westnortheuropee highincomee ///
othercountryy selfreportedd numberofscvariabless agee genderr educationn ///
maritalstatuss incomee olss orhrr hlmm seotherr tnormall tcalculatedbypvaluee , ///
backward bic fix(conss physicalhealthh mentalhealthh individualscc panell ivv)

// Random Effects - Equal weight to each estimate (Column 3)
regress retstatR conss physicalhealthh mentalhealthh individualscc panell ivv  ///
westnortheuropee othercountryy numberofscvariabless agee educationn seotherr ///
tcalculatedbypvaluee if esample == 1, noc vce(cluster id) 
display e(r2_a)		

test (physicalhealthh mentalhealthh)	   

*---------------------------------------------------------*
*   TABLE: RandomEffects(Weight2) // Stepwise Regression  *
*---------------------------------------------------------*
drop esample
quietly regress retstatR repubbiasR pubyearr individualscc panell ivv eastasiaa ///
westnortheuropee highincomee othercountryy physicalhealthh mentalhealthh ///
selfreportedd numberofscvariabless agee genderr educationn maritalstatuss ///
incomee olss orhrr hlmm seotherr tnormall tcalculatedbypvaluee conss ///
[pweight = weight] 
gen esample = e(sample)

vselect retstatR repubbiasR pubyearr eastasiaa westnortheuropee highincomee ///
othercountryy selfreportedd numberofscvariabless agee genderr educationn ///
maritalstatuss incomee olss orhrr hlmm seotherr tnormall tcalculatedbypvaluee ///
[pweight = weight] , backward bic fix(conss physicalhealthh mentalhealthh individualscc panell ivv)

// Random Effects - Equal weight to each study (Column 4)
regress retstatR conss physicalhealthh mentalhealthh individualscc panell ivv  ///
pubyearr eastasiaa westnortheuropee highincomee selfreportedd numberofscvariabless ///
educationn maritalstatuss olss seotherr tnormall tcalculatedbypvaluee [pweight = weight] ///
if esample == 1, noc vce(cluster id)
display e(r2_a)

test (physicalhealthh mentalhealthh)

log close

 